package sdk.webview.fmc.com.fmcsdk.presenter;

import android.os.Build;
import android.text.TextUtils;
import android.util.Log;

import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import sdk.webview.fmc.com.fmcsdk.R;
import sdk.webview.fmc.com.fmcsdk.base.BaseActivity;
import sdk.webview.fmc.com.fmcsdk.base.BaseData;
import sdk.webview.fmc.com.fmcsdk.base.BasePresenter;
import sdk.webview.fmc.com.fmcsdk.base.CommonApi;
import sdk.webview.fmc.com.fmcsdk.base.UserApi;
import sdk.webview.fmc.com.fmcsdk.bean.AgreementBean;
import sdk.webview.fmc.com.fmcsdk.bean.AgreementEntity;
import sdk.webview.fmc.com.fmcsdk.bean.CheckPhoneSmsBean;
import sdk.webview.fmc.com.fmcsdk.bean.IconBean;
import sdk.webview.fmc.com.fmcsdk.bean.LoginInfo;
import sdk.webview.fmc.com.fmcsdk.bean.LoginParamBean;
import sdk.webview.fmc.com.fmcsdk.bean.ModfiyPassword;
import sdk.webview.fmc.com.fmcsdk.bean.ResultBean;
import sdk.webview.fmc.com.fmcsdk.bean.SysSettingsInfoBean;
import sdk.webview.fmc.com.fmcsdk.bean.SystemSetting;
import sdk.webview.fmc.com.fmcsdk.bean.User;
import sdk.webview.fmc.com.fmcsdk.bean.UserAgreement;
import sdk.webview.fmc.com.fmcsdk.bean.XiyouLoginBean;
import sdk.webview.fmc.com.fmcsdk.interfaces.ForgetPwdView;
import sdk.webview.fmc.com.fmcsdk.interfaces.LoginView;
import sdk.webview.fmc.com.fmcsdk.util.AESUtil;
import sdk.webview.fmc.com.fmcsdk.util.Constant;
import sdk.webview.fmc.com.fmcsdk.util.DESCoder;
import sdk.webview.fmc.com.fmcsdk.util.DesUtil;
import sdk.webview.fmc.com.fmcsdk.util.ImageUtils;
import sdk.webview.fmc.com.fmcsdk.util.PreferencesHelper;
import sdk.webview.fmc.com.fmcsdk.util.SM2Utils;
import sdk.webview.fmc.com.fmcsdk.util.StringUtils;
import sdk.webview.fmc.com.fmcsdk.util.ToastUtils;
import sdk.webview.fmc.com.fmcsdk.util.Url;
import sdk.webview.fmc.com.fmcsdk.util.UserHelper;
import sdk.webview.fmc.com.fmcsdk.util.Utility;
import sdk.webview.fmc.com.fmcsdk.util.XLog;
import sdk.webview.fmc.com.fmcsdk.util.retrofit2.observer.ApiFactory;
import sdk.webview.fmc.com.fmcsdk.util.retrofit2.observer.ResponseTransformer;
import sdk.webview.fmc.com.fmcsdk.util.retrofit2.subscriber.ResponseSubscriber;

/**
 * Created by ydm on 2017/8/4.
 */

public class ForgetPwdPresenter extends BasePresenter<ForgetPwdView> {

    /**
     * 手机短信登录 短信验证码生成
     */
    public void sendUserNameMessage(String userName) {
        getApi(UserApi.class).sendUserNameMessage(getDefaultAddress() + Url.SENDFORGETPWDSMS + "?userName=" + userName)
                .compose(new ResponseTransformer<>(this.<ResultBean>bindToLifeCycle()))
                .subscribe(new ResponseSubscriber<ResultBean>(view) {
                    @Override
                    public void success(ResultBean baseData) {
                        if ("200".equals(baseData.getStatus())) {
                            view.sendSmsSuccess(baseData.getRecord());
                        } else {
                            view.sendSmsFail();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        view.sendSmsFail();
                        super.onError(e);
                    }

                    @Override
                    public void onRequestAgain() {
                        view.sendSmsFail();
                    }

                });
    }

    /**
     * 重置密码校验手机号验证码  下一步
     *
     * @param phoneCode
     */
    public void checkForpwdSms(String phoneCode) {
        HashMap<String, String> map = new HashMap();
        map.put("phoneCode", phoneCode);

        Log.e("shuchang", "checkForpwdSms---phoneCode--->" + phoneCode);
        Map<String, RequestBody> stringRequestBodyMap = StringUtils.generateRequestBody(map);

        getApi(UserApi.class).checkFpwSms(getDefaultAddress() + Url.CHECK_FW_SMS, stringRequestBodyMap)
                .compose(new ResponseTransformer<>(this.<CheckPhoneSmsBean>bindToLifeCycle()))
                .subscribe(new ResponseSubscriber<CheckPhoneSmsBean>(view) {
                    @Override
                    public void success(CheckPhoneSmsBean checkPhoneSmsBean) {
                        if (checkPhoneSmsBean.getRecord().isCheck()) {
                            view.checkForpwdSmsSuccess();
                        } else {
                            ToastUtils.showShort(view.getViewContext(), view.getViewContext().getResources().getString(R.string.sms_cuowu_guoqi));
                        }

                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }

                    @Override
                    public void onRequestAgain() {

                    }

                });
    }

    /**
     * accessToken	Long	是	操作令牌
     * username	String	是	用户名
     * loginOldPwd	String	否	旧密码
     * loginNewPwd	String	否	新密码
     * encrypted	String	否	是否加密	3  固定写死
     * type 1 是忘记密码  0不是
     */
    public void editPwd(String username, String loginOldPwd, String loginNewPwd, String type) {

        HashMap<String, String> map = new HashMap();
        map.put("username", username);
        map.put("loginOldPwd", AESUtil.encryptAESExpand(loginOldPwd)); //AES加密
        map.put("loginNewPwd", AESUtil.encryptAESExpand(loginNewPwd));
        map.put("defaultPwd", AESUtil.encryptAESExpand(UserHelper.getSysSettingsValueBycode("PWD_INIT")));  //初shi密码
        map.put("pwdComplexity", UserHelper.getSysSettingsValueBycode("PWD_COMPLEXITY"));  //复杂度
        map.put("pwdLen", UserHelper.getSysSettingsValueBycode("PWD_LEN"));  //密码长度
        map.put("type", type);
        map.put("encrypted", "3");

        Map<String, RequestBody> stringRequestBodyMap = StringUtils.generateRequestBody(map);

        ApiFactory.getFactory().create(UserApi.class).updatePwd(getDefaultAddress() + Url.UPDATE_PWD,
                "", stringRequestBodyMap)
                .compose(new ResponseTransformer<>(this.<BaseData>bindToLifeCycle()))
                .subscribe(new ResponseSubscriber<BaseData>() {
                    @Override
                    public void success(BaseData baseData) {
                        view.updatePwdSuccess();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }

                    @Override
                    public void onRequestAgain() {

                    }

                });
    }

}

